package com.qf.service.impl;

import com.qf.dao.CityMapper;
import com.qf.dao.UserMapper;
import com.qf.pojo.City;
import com.qf.pojo.User;
import com.qf.service.UserService;
import com.qf.vo.BaseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    CityMapper cityMapper;

    @Override
    public BaseResult userLogin(User user, HttpSession session) {
        BaseResult baseResult = new BaseResult();
        User trueUser = userMapper.findUserByTel(user.getTel());

        //没有找到该用户
        if(trueUser==null){
            baseResult.setCode(10000);
            baseResult.setInfo("没有找到该用户!");
            return baseResult;
        }

        //数据库中存在tel相同的用户,判断密码
        if(!trueUser.getPassword().equals(user.getPassword())){
            baseResult.setCode(10001);
            baseResult.setInfo("密码输入有误!");
            return baseResult;
        }

        //查到该用户,登录成功
        baseResult.setCode(1);
        baseResult.setInfo("登录成功!");
        //登录提交的user只有两项字段,在数据库中找到完整user放到session中
        User userByTel = userMapper.findUserByTel(user.getTel());
        session.setAttribute("user",userByTel);
        return baseResult;
    }

    @Override
    public BaseResult userRegister(User user, HttpSession session) {
        BaseResult baseResult = new BaseResult();
        //判断验证码是否一致
        Object captcha = session.getAttribute("captcha");
        //验证码不一致
        if(!captcha.equals(user.getCode())){
            baseResult.setCode(20000);
            baseResult.setInfo("验证码有误!");
            return baseResult;
        }

        //判断手机号是否已经注册过
        User userByTel = userMapper.findUserByTel(user.getTel());
        if(userByTel!=null){
            baseResult.setCode(20001);
            baseResult.setInfo("手机号已注册,请使用未注册的手机号!");
            return baseResult;
        }

        //注册成功
        userMapper.insertUser(user);
        baseResult.setCode(1);
        baseResult.setInfo("注册成功");
        return baseResult;
    }

    @Override
    public BaseResult isLogin(HttpSession session) {
        BaseResult baseResult = new BaseResult();
        User user = (User)session.getAttribute("user");
        if (user==null){
            baseResult.setCode(1000);
            return baseResult;
        }
        baseResult.setCode(1);
        baseResult.setInfo(user);
        return baseResult;
    }

    @Override
    public BaseResult updateTel(String tel,HttpSession session) {
        BaseResult baseResult = new BaseResult();
        User user = (User) session.getAttribute("user");
        if(user==null){
            baseResult.setCode(20000);
            baseResult.setInfo("未登录!");
            return  baseResult;
        }
        user.setTel(tel);
        userMapper.updateTel(user);
        baseResult.setCode(1);
        return baseResult;
    }

    @Override
    public BaseResult updateEmail(String email,HttpSession session) {
        BaseResult baseResult = new BaseResult();
        User user = (User) session.getAttribute("user");
        if(user==null){
            baseResult.setCode(20000);
            baseResult.setInfo("未登录!");
            return  baseResult;
        }
        user.setEmail(email);
        userMapper.updateEmail(user);
        baseResult.setCode(1);
        return baseResult;
    }

    @Override
    public BaseResult updatePassword(String password, String repassword, HttpSession session) {
        BaseResult baseResult = new BaseResult();
        User user = (User) session.getAttribute("user");
        if(password==null || repassword==null){
            baseResult.setCode(10000);
            baseResult.setInfo("密码不能输入为空!");
        }
        if(!password.equals(repassword)){
            baseResult.setCode(10001);
            baseResult.setInfo("两次输入密码不一致!");
        }
        if(user==null){
            baseResult.setCode(20000);
            baseResult.setInfo("未登录!");
            return  baseResult;
        }
        user.setPassword(password);
        userMapper.updatePassword(user);
        baseResult.setCode(1);
        return baseResult;
    }


}
